package xj.graph2d.app.prop;

import java.awt.Container;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JPopupMenu;

import xj.graph2d.ArrowAttr;
import xj.graph2d.Command;
import xj.graph2d.DrawCanvas;
import xj.graph2d.DrawShape;
import xj.graph2d.OpenShape;
import xj.graph2d.SimpleDrawDocument;
import xj.graph2d.SimpleDrawView;
import xj.graph2d.app.DrawApp;
import xj.graph2d.command.SetArrowsCommand;

public class ArrowSelectionListener extends AttrListener implements
    ActionListener {

  // arrow position
  public static final int TAIL_ARROW = 0;

  public static final int HEAD_ARROW = 1;

  public ArrowSelectionListener(int arrowPos, String defaultIconName) {
    this.arrowPos = arrowPos;
    this.defaultIconName = defaultIconName;
  }

  public void init(ArrowIcon icon) {
    this.icon = icon;
    SimpleDrawDocument doc = main.getDoc();
    SimpleDrawView view = main.getView();
    if (arrowPos == TAIL_ARROW) {
      attr = view.getTailArrowAttr();
    } else {
      attr = view.getHeadArrowAttr();
    }
  }

  @Override
  protected void init() {
    SimpleDrawDocument doc = main.getDoc();
    SimpleDrawView view = main.getView();
    if (arrowPos == TAIL_ARROW) {
      attr = view.getTailArrowAttr();
    } else {
      attr = view.getHeadArrowAttr();
    }
    Object ic = DrawApp.getIcon(defaultIconName);
    if (ic instanceof ArrowIcon) {
      icon = (ArrowIcon) ic;
    }
  }

  public void actionPerformed(ActionEvent e) {
    SimpleDrawDocument doc = main.getDoc();
    SimpleDrawView view = main.getView();
    if (arrowPos == TAIL_ARROW) {
      attr = view.getTailArrowAttr();
    } else {
      attr = view.getHeadArrowAttr();
    }

    if (popup == null) {
      initPopup();
    }
    Object source = e.getSource();
    if (source instanceof AbstractButton) {
      comp = (AbstractButton) source;
    }
    if (popup != null && comp != null) {
      int h = comp.getHeight();
      popup.show(comp, 0, h);
    }

    if (comp != null) {
      comp.setSelected(false);
    }
  }

  class ArrowAction implements ActionListener {

    public void actionPerformed(ActionEvent e) {
      Object source = e.getSource();
      if (source instanceof JButton) {
	JButton button = (JButton) source;
	Container p = button.getParent();
	p.setVisible(false);
	ArrowIcon ic = (ArrowIcon) button.getIcon();
	ArrowAttr.ArrowType arrowType = ic.getArrowType();
	if (icon != null && comp != null) {
	  icon.setArrowType(arrowType);
	  comp.repaint();
	}

	if (updateSystemAttr) {
	  // update system attribute
	  attr.setType(arrowType);
	}

	// update current shape
	SimpleDrawDocument doc = main.getDoc();
	SimpleDrawView view = main.getView();
	if (doc != null) {
	  DrawShape curShape = doc.getSelectedShape();
	  if (curShape instanceof OpenShape) {
	    OpenShape oshape = (OpenShape) curShape;
	    Command cmd = new SetArrowsCommand(view, doc, curShape);
	    doc.addCommand(cmd);

	    ArrowAttr sattr = null;
	    if (updateSystemAttr) {
	      sattr = attr;
	    } else {
	      if (arrowPos == TAIL_ARROW) {
		sattr = oshape.getTailArrow();
	      } else {
		sattr = oshape.getHeadArrow();
	      }
	      if (sattr == null) {
		sattr = (ArrowAttr) attr.clone();
	      }
	      sattr.setType(arrowType);
	    }
	    if (arrowPos == TAIL_ARROW) {
	      oshape.setTailArrow(sattr);
	    } else {
	      oshape.setHeadArrow(sattr);
	    }
	    curShape.calculateBounds();
	    DrawCanvas canvas = view.getCanvas();
	    canvas.repaintShape(curShape);
	  }
	}
      }
    }
  }

  protected void initPopup() {
    ActionListener act = new ArrowAction();

    popup = new JPopupMenu();
    JButton mi;
    ArrowIcon icon;
    for (ArrowAttr.ArrowType t : ArrowAttr.ArrowType.values()) {
      if (arrowPos == TAIL_ARROW) {
	icon = new TailArrowIcon();
      } else {
	icon = new HeadArrowIcon();
      }
      icon.setArrowType(t);
      mi = new JButton(icon);
      mi.setMargin(new Insets(0, 0, 0, 0));
      // mi.setBorder(null);
      mi.addActionListener(act);
      popup.add(mi);
    }
  }

  protected int arrowPos = TAIL_ARROW;

  protected String defaultIconName = null;

  protected ArrowIcon icon = null;

  protected ArrowAttr attr = null;

  protected JPopupMenu popup = null;

  protected AbstractButton comp = null;

}
